package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.OrganizationBase;
import com.atguigu.eduservice.entity.SystemOrganizationBase1;
import com.atguigu.eduservice.mapper.RealLandBaseMapper;
import com.atguigu.eduservice.service.DwService;
import com.atguigu.eduservice.service.OrgExcelService;
import com.atguigu.eduservice.service.OrganizationBaseService;
import com.atguigu.eduservice.service.RealLandBaseService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "机构表格导入")
@RequestMapping("orgExcel")
@RestController
public class OrgExcelController {
    @Resource
    private OrgExcelService orgExcelService;

    @Resource
    private DwService dwService;

    @Resource
    private OrganizationBaseService organizationBaseService;


    @Resource
    private RealLandBaseService realLandBaseService;

    @Resource
    private RealLandBaseMapper realLandBaseMapper;


    /**
     * excel导入机构表
     * @param file
     * @return
     */
    @PostMapping("/add")
    public R addExcel(MultipartFile file) {
        //上传过来excel文件
        orgExcelService.saveOrgExcel(file,orgExcelService);
        return R.ok();
    }


    /**
     * 生成机构编号和父机构编号
     * 001、001001   001001001这种格式
     * @return
     */
    @PostMapping("getAll")
    public R getAll() {
        List<SystemOrganizationBase1> all = orgExcelService.getAll();
        return R.ok().data("data",all);
    }

    /**
     * 根据机构名称更新机构，把新表的数据导入到旧表里，如果旧表没有相对应的数据则添加，有就修改
     * @return
     */

    @PostMapping("updOrIns")
    public R updOrIns() {
       orgExcelService.updOrIns();
        return R.ok();
    }


    @GetMapping("/org")
    public R getOrg(){
        Map org = dwService.getOrg();
        return R.ok().data(org);
    }

    @PostMapping("getAllMap")
    public R getAllMap() {
        Map all = orgExcelService.getAllMap();
        return R.ok().data("data",all);
    }

    /**
     * 获取树形结构
     * @return
     */
    @GetMapping("/orgTree")
    public R getOrgTree(){

        List<OrganizationBase> organizationBases = organizationBaseService.listWithTree();

        return R.ok().data("data",organizationBases);
    }



    /**
     * 土地导入
     * @param file
     * @return
     */
    @PostMapping("/addLandExcel")
    public R addLandExcel(MultipartFile file) {
        String sheetNameVal = "土地数据收集模板";
        //上传过来excel文件
        realLandBaseService.saveLandExcel(file,realLandBaseService,realLandBaseMapper,sheetNameVal);

        return R.ok();
    }

    /**
     * 房产导入
     * @param file
     * @return
     */
    @PostMapping("/addLandExcel1")
    public R addLandExcel1(MultipartFile file) {
        String sheetNameVal = "房产数据收集模板";
        //上传过来excel文件
        realLandBaseService.saveLandExcel(file,realLandBaseService,realLandBaseMapper,sheetNameVal);

        return R.ok();
    }













}
